package com.b1502.store2.fragment;


import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.b1502.store2.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private View view;
    private RecyclerView rv_message_news;
    private newsAdapter news;
    private LinearLayoutManager linearLayoutManager;


    public NewsFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NewsFragment newInstance(String param1, String param2) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_news, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        int spacingInPixels=8;

        rv_message_news = (RecyclerView) view.findViewById(R.id.rv_message_news);

        rv_message_news.addItemDecoration(new SpacesItemDecoration(spacingInPixels));

        linearLayoutManager = new LinearLayoutManager(getActivity());

        rv_message_news.setLayoutManager(linearLayoutManager);

        news = new newsAdapter();

        rv_message_news.setAdapter(news);

    }

    class SpacesItemDecoration extends RecyclerView.ItemDecoration{

        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;

            if (parent.getChildPosition(view)==0){

                outRect.top=space;

            }

        }
    }

    class newsAdapter extends RecyclerView.Adapter<newsAdapter.MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                    getActivity()).inflate(R.layout.recycleview_news, parent,
                    false));

            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            holder.iv_picture_news.setImageResource(R.mipmap.ic_launcher);

            holder.tv_description_news.setText("11111");

        }

        @Override
        public int getItemCount() {
            return 6;
        }

        class MyViewHolder extends RecyclerView.ViewHolder
        {

            ImageView iv_picture_news;

            TextView tv_description_news;

            public MyViewHolder(View view)
            {
                super(view);

                tv_description_news = (TextView) view.findViewById(R.id.tv_description_news);

                iv_picture_news= (ImageView) view.findViewById(R.id.iv_picture_news);

            }
        }

    }
}
